﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushFormsApp
{

    /// <summary>
    /// 文件监控类，用于监控指定目录下文件以及文件夹的变化
    /// </summary>
    public class FileWatcher
    {
        private FileSystemWatcher _watcher = null;
        private string _path = string.Empty;
        private string _filter = string.Empty;
        private bool _isWatch = false;
        public event FileSystemEventHandler Deleted = null;
        public event FileSystemEventHandler CreatedDolder = null;
        public event FileSystemEventHandler CreatedFile = null;
        public event FileSystemEventHandler Changed = null;
        public event RenamedEventHandler Renamed = null;

        /// <summary>
        /// 监控是否正在运行
        /// </summary>
        public bool IsWatch
        {
            get
            {
                return _isWatch;
            }
        }

        /// <summary>
        /// 初始化FileWatcher类
        /// </summary>
        /// <param name="path">监控路径</param>
        public FileWatcher(string path)
        {
            _path = path;
        }

        /// <summary>
        /// 初始化FileWatcher类，并指定是否监控指定类型文件
        /// </summary>
        /// <param name="path">监控路径</param>
        /// <param name="filter">指定类型文件，格式如:*.txt,*.doc,*.rar</param>
        public FileWatcher(string path, string filter)
        {
            _path = path;
            _filter = filter;
        }

        /// <summary>
        /// 打开文件监听器
        /// </summary>
        public void Open()
        {
            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }

            if (string.IsNullOrEmpty(_filter))
            {
                _watcher = new FileSystemWatcher(_path);
            }
            else
            {
                _watcher = new FileSystemWatcher(_path, _filter);
            }
            //注册监听事件
            _watcher.Created += new FileSystemEventHandler(OnProcess);
            _watcher.Changed += new FileSystemEventHandler(OnProcess);
            _watcher.Deleted += new FileSystemEventHandler(OnProcess);
            _watcher.Renamed += new RenamedEventHandler(OnFileRenamed);
            _watcher.IncludeSubdirectories = true;
            _watcher.EnableRaisingEvents = true;
            _isWatch = true;
        }

        /// <summary>
        /// 关闭监听器
        /// </summary>
        public void Close()
        {
            _isWatch = false;
            _watcher.Created -= new FileSystemEventHandler(OnProcess);
            _watcher.Changed -= new FileSystemEventHandler(OnProcess);
            _watcher.Deleted -= new FileSystemEventHandler(OnProcess);
            _watcher.Renamed -= new RenamedEventHandler(OnFileRenamed);
            _watcher.EnableRaisingEvents = false;
            _watcher = null;
        }

        /// <summary>
        /// 监听事件触发的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnProcess(object sender, FileSystemEventArgs e)
        {
            try
            {
                if (e.ChangeType == WatcherChangeTypes.Created)
                {
                    if (System.IO.File.GetAttributes(e.FullPath) == FileAttributes.Directory)
                    {
                        //创建文件夹
                        CreatedDolder?.Invoke(sender, e);
                    }
                    else
                    {
                        //创建文件
                        CreatedFile?.Invoke(sender, e);
                    }
                }
                else if (e.ChangeType == WatcherChangeTypes.Changed)
                {
                    //文件修改
                    Changed?.Invoke(sender, e);
                }
                else if (e.ChangeType == WatcherChangeTypes.Deleted)
                {
                    //文件删除
                    Deleted?.Invoke(sender, e);
                }

            }
            catch (Exception ex)
            {
                Close();
            }
        }

        /// <summary>
        /// 文件或目录重命名时触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnFileRenamed(object sender, RenamedEventArgs e)
        {
            try
            {
                Renamed?.Invoke(sender, e);
            }
            catch
            {
                Close();
            }
        }
    }
}
